<template>
  <div>
    <!-- 增加药品分类 -->
    <div>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm" label-position="right">
        <el-form-item label="名称：" prop="dataName">
          <el-input v-model="ruleForm.dataName" />
        </el-form-item>
        <!-- <el-form-item> -->
        <!-- <el-button class="submit" :loading="loading" @click="submitForm('ruleForm')">保存</el-button> -->
        <!-- </el-form-item> -->
        <el-form-item label="状态：" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio label="启用" />
            <el-radio label="停用" />
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    row: {
      type: Object,
      default: () => ({}),
    },
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      value: '',
      options: [],
      ruleForm: {
        dataName: '',
        status: "启用",
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],
      },
      loading: false,
    };
  },
  watch: {
    isEdit: {
      handler(val) {
        if (val && this.row.id) {
          this.ruleForm = this.row;
        }
      },
      immediate: true, // 立即触发
    },
  },
  mounted() {
    console.log(this.row, this.isEdit);
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return;
        this.loading = true;
        if (!this.isEdit) {
          this.$api['data/goods/goodsTreeAdd']({
            ...this.ruleForm,
            dataLevel: this.row.dataLevel ? (Number(this.row.dataLevel) + 1) : 0,
            pid: this.row.id ? this.row.id : 0,
            dataType: '药品类',
          }).then(res => {
            if (res.code !== 200) return;
            this.$emit('success');
            this.msgSuccess('增加药品分类成功');
          }).finally(() => (this.loading = false));
        } else {
          this.$api['data/goods/goodsTreeUpdate']({
            ...this.ruleForm,
          }).then(res => {
            if (res.code !== 200) return;
            this.$emit('success');
            this.$emit('update:isEdit', false);
            this.msgSuccess('修改药品分类成功');
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.dialog-magin-y{
  margin: 0 150px;
}
</style>
